<?php
namespace app\index\controller;

use think\Controller;
use think\Db;

class Index extends Controller
{
    protected $db2 =    [
        // 数据库类型
        'type'            => 'mysql',
        // 服务器地址
        'hostname'        => '127.0.0.1',
        // 数据库名
        'database'        => 'sudaiqianbao',
        // 用户名
        'username'        => 'root',
        // 密码
        'password'        => '123456',
        // 端口
        'hostport'        => '',
        // 连接dsn
        'dsn'             => '',
        // 数据库连接参数
        'params'          => [],
        // 数据库编码默认采用utf8
        'charset'         => 'utf8',
        // 数据库表前缀
        'prefix'          => 'sd_',
        // 数据库调试模式
        'debug'           => true,
        // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
        'deploy'          => 1,
        // 数据库读写是否分离 主从式有效
        'rw_separate'     => false,
        // 读写分离后 主服务器数量
        'master_num'      => 1,
        // 指定从服务器序号
        'slave_no'        => '',
        // 是否严格检查字段是否存在
        'fields_strict'   => true,
        // 数据集返回类型
        'resultset_type'  => 'array',
        // 自动写入时间戳字段
        'auto_timestamp'  => false,
        // 时间字段取出后的默认时间格式
        'datetime_format' => 'Y-m-d H:i:s',
        // 是否需要进行SQL性能分析
        'sql_explain'     => false,
    ];

    public function index()
    {
        return view();
    }

    public function save_member_info ()
    {

        $get['real_name'] = input('get.real_name', '', 'trim,htmlspecialchars');
        $get['idcard'] = input('get.idcard', '', 'trim,htmlspecialchars');
        $get['phone'] = input('get.phone', '', 'trim,htmlspecialchars');

        $hadIdCard = Db::connect($this->db2)->name('member')->where('idcard', $get['idcard'])->find();

        if ($hadIdCard) {
            $find = db('member')->where('idcard' , $hadIdCard['idcard'])->find();
            if (!$find) {
                $men_id = db('member')->insertGetId($get);
                $real_name = $get['real_name'];
                $idcard = $get['idcard'];
                $phone = $get['phone'];
            }else {
                $men_id = $find['id'];
                $real_name = $get['real_name'];
                $idcard = $get['idcard'];
                $phone = $get['phone'];
            }
        }elseif (!$hadIdCard) {
            $hadIdCard = db('member')->where('idcard', $get['idcard'])->find();

            if (!$hadIdCard) {
            // 身份证实名
            $url = 'http://op.juhe.cn/idcard/query?key=703f705fab1042d8c5295289d5e5cfab&idcard=' . $get['idcard'] . '&realname=' . $get['real_name'];

            $res = get($url);
            $res = json_decode($res, true);
            if ($res['error_code'] == 0) {
                $men_id = db('member')->insertGetId($get);
                $real_name = $get['real_name'];
                $idcard = $get['idcard'];
                $phone = $get['phone'];
            } else {
                return json(['status' => false, 'msg' => '身份证不匹配']);
            }

        } else {

            $men_id = $hadIdCard['id'];

            $real_name = $hadIdCard['real_name'];
            $idcard = $hadIdCard['idcard'];
            $phone = $hadIdCard['phone'];
        }
    }
        session('member_id' , $men_id);
        session('real_name' , $real_name);
        session('idcard' , $idcard);
        session('phone' , $phone);
        return json(['status' => true]);
    }

    public function select_item ()
    {
        $item = db('item')->order('ord' , 'asc')->select();
        $tmp_item = $item;

        foreach ($tmp_item as $k1 => $v1)
        {
            foreach ($tmp_item as $k2 => $v2)
            {
                if ($v2['pid'] == $v1['id'])
                {
                    $item[$k1]['son'][] = $v2;
                    unset($item[$k2]);
                }
            }
        }
        $this->assign('item' , $item);
        return view();
    }


    // 获取并保存用于的选项
    public function save_item ()
    {
        $ids = explode('-' , input('get.id'));
        $member_id = session('member_id');

        db('memberItem')->where('member_id' , $member_id)->delete();
        foreach($ids as $key => $value) {
            db('memberItem')->insert(['member_id' => $member_id , 'item'=>$value] );
        }
        $count = db('item')->where(['id'=>['in' , implode(',' , $ids)]])->select();
        $price = 0;
        foreach ($count as $key => $value)
        {
            $price += $value['price'];
        }
        $price = $price > 0 ? $price : 0;
        session('price' , $price);
        // 跳转
        $this->redirect(url('get_jie'));
    }

    // 用户勾选同意协议后的方法
    public function moremessage ()
    {
       return view('member_card');
    }

    // 会员卡
    public function member_card ()
    {
        return view();
    }

    public function get_jie ()
    {
        return view();
    }

    public function xieyi ()
    {
        return view();
    }
    public function pay ()
    {
        $pri = input('pri' , '299');
        $qrcode = db('config')->where(['name'=>'qrcode'])->find();
        $qrcode = $qrcode['value'];
        if (!$qrcode)
        {
            $qrcode = '__STATIC__/jishidai/images/WechatIMG35.jpeg';
        }
        $this->assign('qrcode', $qrcode);
        $this->assign('pri', $pri);
        return view();
    }
}
